<template>
  <div class="theme-switch">
    <el-dropdown @command="handleThemeChange">
      <el-button class="theme-button" circle>
        <el-icon v-if="currentTheme === 'light'"><Sunny /></el-icon>
        <el-icon v-else-if="currentTheme === 'dark'"><Moon /></el-icon>
        <el-icon v-else><Monitor /></el-icon>
      </el-button>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item 
            :command="THEME.LIGHT" 
            :disabled="currentTheme === THEME.LIGHT"
          >
            <el-icon><Sunny /></el-icon>
            <span>亮色主题</span>
          </el-dropdown-item>
          <el-dropdown-item 
            :command="THEME.DARK" 
            :disabled="currentTheme === THEME.DARK"
          >
            <el-icon><Moon /></el-icon>
            <span>暗色主题</span>
          </el-dropdown-item>
          <el-dropdown-item 
            :command="THEME.AUTO" 
            :disabled="currentTheme === THEME.AUTO"
          >
            <el-icon><Monitor /></el-icon>
            <span>跟随系统</span>
          </el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>

<script>
import { computed } from 'vue'
import { Sunny, Moon, Monitor } from '@element-plus/icons-vue'
import { useThemeStore } from '../stores/modules'
import { setTheme, THEME } from '../utils/theme'

export default {
  name: 'ThemeSwitch',
  components: {
    Sunny,
    Moon,
    Monitor
  },
  setup() {
    const themeStore = useThemeStore()
    
    // 使用计算属性从Pinia store中获取当前主题
    const currentTheme = computed(() => themeStore.theme)
    
    // 处理主题切换
    const handleThemeChange = (theme) => {
      themeStore.setTheme(theme)
      setTheme(theme)
    }
    
    return {
      currentTheme,
      THEME,
      handleThemeChange
    }
  }
}
</script>

<style scoped>
.theme-switch {
  display: inline-block;
  margin-left: 20px;
}

.theme-button {
  border: none;
  background-color: transparent;
  font-size: 18px;
  padding: 8px;
}
</style>